# Create map ----
map <- leaflet() |>
addProviderTiles(providers$CartoDB.Positron) |>
# Add ocean polygons (no popups) ----
addPolygons(
data = wgs84_geo$marine_polys |> filter(highlight_name),
fillColor = ~ ifelse(
highlight_name == TRUE,
marine_colors[["highlight"]],
marine_colors[["default"]]
),
fillOpacity = ~ ifelse(highlight_name == TRUE, 0.3, 0),
color = "black",
weight = 1,
group = "Geography"
) |>
# Add country polygons (no popups) ----
addPolygons(
data = wgs84_geo$countries |> filter(highlight_name),
fillColor = ~ ifelse(
highlight_name == TRUE,
country_colours[["highlight"]],
country_colours[["default"]]
),
fillOpacity = ~ ifelse(highlight_name == TRUE, 0.3, 0),
weight = 1,
color = "black",
group = "Geography"
) |>
# Add permanent labels for oceans ----
addLabelOnlyMarkers(
data = marine_centroids,
label = ~name,
labelOptions = labelOptions(
noHide = TRUE,
direction = "center",
textOnly = TRUE,
style = list(
"color" = "darkblue",
"font-weight" = "bold",
"font-size" = "14px",
"text-shadow" = "1px 1px 2px white, -1px -1px 2px white"
)
),
group = "Geography"
) |>
# Add permanent labels for countries ----
addLabelOnlyMarkers(
data = country_centroids,
label = ~name,
labelOptions = labelOptions(
noHide = TRUE,
direction = "center",
textOnly = TRUE,
style = list(
"color" = "darkgreen",
"font-weight" = "bold",
"font-size" = "12px",
"text-shadow" = "1px 1px 2px white, -1px -1px 2px white"
)
),
group = "Geography"
) |>
# Add study connection lines ----
addPolylines(
data = study_lines_sf,
color = "purple",
weight = 2,
opacity = 0.5,
dashArray = "5, 5",
label = ~ paste0("Study: ", REFERENCE_ID),
labelOptions = labelOptions(
style = list("font-weight" = "normal")
),
group = "Study Connections"
) |>
# Add mine tailings markers ----
addMarkers(
data = mine_tailings_coords(),
lng = ~LONGITUDE,
lat = ~LATITUDE,
icon = list(
iconUrl = "https://upload.wikimedia.org/wikipedia/commons/8/8c/Mining_symbol.svg",
iconSize = c(15, 15)
),
popup = ~ paste0(
"<b>Mine:</b> ",
MINE_NAME,
"<br>",
"<b>Location:</b> ",
SITE_NAME,
"<br>",
"<b>Ore Type:</b> ",
ORE_TYPE,
"<br>",
"<b>Status:</b> ",
ifelse(ACTIVE_2013, "Active (2013)", "Terminated"),
"<br>",
"<b>Details:</b> ",
KEY_DETAILS,
"<br>",
"<b>Coordinate Confidence:</b> ",
CONFIDENCE,
"/5"
),
group = "Mine Tailings"
) |>
# apple markers... for apples
addMarkers(
data = apple_data,
lng = ~x,
lat = ~y,
icon = list(
iconUrl = "https://www.svgrepo.com/show/42619/apple-fruit.svg",
iconSize = c(15, 15)
),
popup = ~ paste0(
"<b>Municipality:</b> ",
name,
"<br>",
"<b>Average Copper Applied:</b> ",
round(avg_copper_kg, 1),
" kg/year",
"<br>",
"<b>Period:</b> 2020-2024"
),
group = "Apple Production"
) |>
# add case study sites
addCircles(
data = expect_case_studies(),
lng = ~lon,
lat = ~lat,
radius = 100000, # 100km radius
color = "hotpink",
fillColor = "hotpink",
fillOpacity = 0.3,
opacity = 0.6,
weight = 2,
popup = ~ paste0(
"<strong>",
site_name,
"</strong><br/>",
"<em>Lat: ",
lat,
", Lon: ",
lon,
"</em><br/><br/>",
summary
),
group = "Potential Case Studies"
)
# Add layers for each main compartment ----
for (comp in environ_compartments_vocabulary) {
data_subset <- data_sf |>
filter(ENVIRON_COMPARTMENT == comp)
if (nrow(data_subset) > 0) {
# Build popup HTML with conditional species info
popup_html <- if (comp == "Biota") {
~ paste0(
"<b>Reference:</b> ",
REFERENCE_ID,
"<br>",
"<b>Site:</b> ",
SITE_CODE,
"<br>",
"<b>Date:</b> ",
SAMPLING_DATE,
"<br>",
"<b>Compartment:</b> ",
ENVIRON_COMPARTMENT,
"<br>",
"<b>Sub-compartment:</b> ",
ENVIRON_COMPARTMENT_SUB,
" (",
SUBCOMP_CODE,
")",
"<br>",
"<b>Species:</b> ",
SAMPLE_SPECIES,
"<br>",
"<b>Measured Value:</b> ",
round(MEASURED_VALUE_STANDARD, 3),
" ",
MEASURED_UNIT_STANDARD
)
} else {
~ paste0(
"<b>Reference:</b> ",
REFERENCE_ID,
"<br>",
"<b>Site:</b> ",
SITE_CODE,
"<br>",
"<b>Date:</b> ",
SAMPLING_DATE,
"<br>",
"<b>Compartment:</b> ",
ENVIRON_COMPARTMENT,
"<br>",
"<b>Sub-compartment:</b> ",
ENVIRON_COMPARTMENT_SUB,
" (",
SUBCOMP_CODE,
")",
"<br>",
"<b>Measured Value:</b> ",
round(MEASURED_VALUE_STANDARD, 3),
" ",
MEASURED_UNIT_STANDARD
)
}
map <- map |>
addCircleMarkers(
data = data_subset,
fillColor = ~ value_pal(MEASURED_VALUE_STANDARD),
color = compartment_pal(comp),
weight = 2,
fillOpacity = 0.7,
radius = 6,
label = ~SUBCOMP_CODE,
labelOptions = labelOptions(
noHide = TRUE,
direction = "center",
textOnly = TRUE,
style = list(
"color" = compartment_pal(comp),
"font-weight" = "normal",
"font-size" = "8px"
)
),
popup = popup_html,
group = "Samples"
)
}
}
# Add site labels as separate toggleable layer ----
map <- map |>
addLabelOnlyMarkers(
data = data_sf,
label = ~SITE_CODE,
labelOptions = labelOptions(
noHide = FALSE,
direction = "top",
textOnly = TRUE,
style = list(
"color" = "black",
"font-size" = "9px",
"background" = "rgba(255, 255, 255, 0.7)",
"padding" = "2px"
)
),
group = "Site Labels"
) |>
# Add layer controls ----
addLayersControl(
baseGroups = character(0),
overlayGroups = c(
"Geography",
"Study Connections",
"Mine Tailings",
"Apple Production",
"Samples",
"Site Labels",
"Potential Case Studies"
),
options = layersControlOptions(collapsed = FALSE)
) |>
# Add legends ----
addLegend(
position = "bottomright",
pal = compartment_pal,
values = environ_compartments_vocabulary,
title = "Compartment<br>(border colour)",
opacity = 1
) |>
addLegend(
position = "bottomleft",
pal = value_pal,
values = data_sf$MEASURED_VALUE_STANDARD,
title = "Measured Value<br>(fill colour)",
opacity = 0.8,
labFormat = labelFormat(digits = 1)
)
# Display map ----
bslib::card(
full_screen = TRUE,
map,
style = "text-align: left; z-index: 999999;"
) # yes, of course, that makes perfect sense